//
//  XNoteBookTableViewCell.m
//  iPlan
//
//  Created by 侯伟 on 2021/9/7.
//

#import "XNoteBookTableViewCell.h"
#import "XNoteBook.h"

@interface XNoteBookTableViewCell()

@property(nonatomic,strong)MyRelativeLayout *contentLayout;

/// 名称
@property(nonatomic,strong)UILabel* titleLb;

/// 占位文本
@property(nonatomic,strong)UILabel* updateLb;

/// 数量
@property(nonatomic,strong)UILabel* contentLb;

/// 选择
@property(nonatomic,strong)UIButton* selectBtn;


@end

@implementation XNoteBookTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = PX(5);
        
        [self initSubViews];
        
        self.editable = NO;
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 10;
    frame.size.width -= 2 * 10;
    [super setFrame:frame];
}

- (void)initSubViews{
    
    MyRelativeLayout *contentLayout = [MyRelativeLayout new];
    contentLayout.cacheEstimatedRect = YES;
    contentLayout.padding = UIEdgeInsetsMake(16, 16, 16, 16);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    UILabel* titleLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_BLACK_DARK text:@"--"];
    titleLb.myTop = 0;
    titleLb.myHorzMargin = 0;
    [contentLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    UIButton* selectBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [selectBtn setImage:[UIImage imageNamed:@"a_select_n"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"a_select_h"] forState:UIControlStateSelected];
    selectBtn.mySize = CGSizeMake(PX(28), PX(28));
    selectBtn.centerYPos.equalTo(titleLb.centerYPos);
    selectBtn.myRight = 0;
    [selectBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    [contentLayout addSubview:selectBtn];
    _selectBtn = selectBtn;
    
    UILabel* contentLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"--"];
    contentLb.topPos.equalTo(titleLb.bottomPos).offset(12);
    contentLb.myHorzMargin = 0;
    contentLb.myHeight = MyLayoutSize.wrap;
    contentLb.numberOfLines = 2;
    [contentLayout addSubview:contentLb];
    _contentLb = contentLb;
    
    
    UILabel* timeLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"--"];
    timeLb.topPos.equalTo(contentLb.bottomPos).offset(20.0);
    timeLb.myHorzMargin = 0;
    [contentLayout addSubview:timeLb];
    _updateLb = timeLb;
    
}

- (void)setEditable:(BOOL)editable{
    _editable = editable;
    
    _selectBtn.visibility = editable?MyVisibility_Visible:MyVisibility_Gone;
    [_selectBtn sizeToFit];
}

- (void)setNoteBook:(XNoteBook *)noteBook{
    _noteBook = noteBook;
    
    self.titleLb.text = noteBook.title;
    [self.titleLb sizeToFit];
    
    self.updateLb.text = [noteBook.update stringWithFormat:@"yyyy.MM.dd "];
    [self.updateLb sizeToFit];
    
    self.contentLb.text = noteBook.content;
    [self.contentLb sizeToFit];
    
    _selectBtn.selected = noteBook.isSelected;
}

- (void)selectClick:(UIButton*)sender{
    sender.selected = !sender.selected;
    
    self.noteBook.isSelected = sender.selected;
    
    if(self.selectBlock){
        self.selectBlock(self.noteBook.isSelected);
    }
}

#pragma private -- Size

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
    
    CGSize cellSize = CGSizeZero;
    if (@available(iOS 11.0, *)) {
        cellSize = [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width - self.safeAreaInsets.left - self.safeAreaInsets.right - 2*10, targetSize.height)];
    } else {
        CGSize size = targetSize;
        size.width = size.width - 2*10;
        cellSize = [self.contentLayout sizeThatFits:size];  //如果使用系统自带的分割线，请记得将返回的高度height+1
    }
    return cellSize;
}
@end
